home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 June / CHIP Haziran 2001.iso / prog / share / 04 / setup.exe / MM7.Cab / F453_utils.runtime5.asp.3A7A5E73_1F50_4F88_AB38_531BFA72E747 < prev    next >
Text File  |  2000-08-17  |  20KB  |  636 lines

  1. <% Response.Buffer = true %>
  2.  
  3. <SCRIPT runat="server" language="JScript">
  4.  
  5. // *****************************************************************************
  6. //
  7. // include/utils.runtime5.asp
  8. //
  9. // Dynamic Link utilities for ASP.
  10. //
  11. // COPYRIGHT (c) 1999-2000 Adobe Systems Incorporated. All rights reserved.
  12.  
  13.  
  14. // *****************************************************************************
  15. // CONTENT SOURCE WRAPPERS
  16. //
  17. // GoLive supports a standard interface definition for a content source.  Other
  18. // objects are wrapped in this interface to allow access.
  19. //
  20. // Content Source Wrapper Properties:
  21. //
  22. //      RecordCount
  23. //         the number of records in the recordset
  24. //      AbsolutePosition
  25. //         the (1-based) index of the current record (the cursor position)
  26. //      EOF
  27. //         true if the cursor is after the last record
  28. //
  29. // Content Source Wrapper Methods:
  30. //
  31. //      Move(delta)
  32. //         moves the cursor by the specified amount
  33. //      MoveFirst()
  34. //         moves the cursor to the first record
  35. //      MoveNext()
  36. //         moves the cursor to the next record
  37. //
  38. //      Value(fieldName)
  39. //         returns the value of a field of the current record
  40. //      Set(fieldName, value)
  41. //         sets the value of a field of the current record
  42. //
  43. //      UpdateBatch()
  44. //         performs any required action to update the datasource after the
  45. //         records have been changed (ie: store back to the database)
  46. //
  47. // Optional Content Source Wrapper Methods:
  48. //
  49. //      Key()
  50. //         returns a key indentifying the current record
  51. //
  52.  
  53. // *****************************************************************************
  54. // Runtime Debug Switch
  55. //    
  56. //    false(default) or true
  57.  
  58. var RuntimeDebug = false;    // true or false (default)
  59.  
  60. // -----------------------------------------------------------------------------
  61. // Methods shared by many Content Source Wrappers.
  62.  
  63. function CSW_Move( delta )
  64. {
  65.     this.AbsolutePosition += delta;
  66.     this.EOF = ( this.AbsolutePosition > this.RecordCount );
  67. }
  68.  
  69. function CSW_MoveFirst()
  70. {
  71.     this.AbsolutePosition = 1;
  72.     this.EOF = ( this.AbsolutePosition > this.RecordCount );
  73. }
  74.  
  75. function CSW_MoveNext()
  76. {
  77.     this.Move( 1 );
  78. }
  79.  
  80. function CSW_NOP()
  81. {
  82. }
  83.  
  84. // *****************************************************************************
  85. // UTILITY FUNCTIONS
  86. //
  87.  
  88.  
  89. // -----------------------------------------------------------------------------
  90. // Get the database path. The only tricky part is  finding the path to the 
  91. // database folder.     The algorithm is to search for the #include of 
  92. // include/ado.runtime5.asp in the current file. If you'd like to set custom path
  93. // out of config folder, you can change as following:
  94. //     return "C:\\databasefolder\\";
  95.  
  96. function GetDatabasePath()
  97. {
  98.     var fileSystem = new ActiveXObject( "Scripting.FileSystemObject" );
  99.     var databasePath = "";
  100.  
  101.     var pathTranslated = String( Request.ServerVariables( "PATH_TRANSLATED" ));
  102.     if( 0 <= pathTranslated.indexOf( "parseasp.asp" )){        // for Server.Transfer()
  103.         databasePath = Server.MapPath(".") + "/../databases/";
  104.     }else{
  105.         var currentFile = fileSystem.OpenTextFile( pathTranslated );
  106.         var matchExpression = /\s*[<]!--\s*#INCLUDE\s+(FILE|VIRTUAL)="(.*)[\/]include[\/]utils.runtime5.asp"\s*--[>]/;
  107.         while( !currentFile.AtEndOfStream ){
  108.             if( matchExpression.exec( currentFile.ReadLine() )){
  109.                 break;
  110.             }
  111.         }
  112.         if( currentFile.AtEndOfStream ){
  113.             return "";
  114.         }
  115.         if( RegExp.$1 == "FILE" ){
  116.             databasePath = Server.MapPath( "." )
  117.                 + "/" + RegExp.$2 + ( RegExp.$2 != "" ? "/":"" )
  118.                 + "databases/";
  119.         } else {
  120.             databasePath = Server.MapPath( RegExp.$2 ) + ( RegExp.$2 != "" ? "/":"" )
  121.                 + "databases/";
  122.         }
  123.     }
  124.     return databasePath;
  125. }
  126.  
  127.  
  128. // -----------------------------------------------------------------------------
  129. // Get the connection string for a database.
  130.  
  131. function ConnectString( db ){    return GetConnectString( db ); }    // version 1.0 compatibility
  132. function GetConnectString( db )
  133. {
  134.     var fileSystem = new ActiveXObject( "Scripting.FileSystemObject" );
  135.     var databasePath = GetDatabasePath();
  136.     if( databasePath == "" )    return;
  137.     databasePath += db;
  138.  
  139.     if( fileSystem.FileExists( databasePath + ".udl" )){
  140.         return "File Name=" + databasePath + ".udl";
  141.     }else if( fileSystem.FileExists( databasePath + ".dsn" )){
  142.         return "FILEDSN=" + databasePath + ".dsn";
  143.     }else if( fileSystem.FileExists( databasePath + ".mdb" )){
  144.         return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + databasePath + ".mdb";
  145.     }else if( fileSystem.FileExists(databasePath + ".xdb" )){
  146.         return "DSN=" + fileSystem.GetBaseName( databasePath );
  147.     }else{
  148.         return "";
  149.     }
  150. }
  151.  
  152.  
  153. // *****************************************************************************
  154. // DYNAMIC LINKING
  155.  
  156. // -----------------------------------------------------------------------------
  157. // Add a URLArgs function to the list of functions to be called by the standard
  158. // URLArgs().
  159.  
  160. var URLArgsFunctionList = new Array;
  161.  
  162. function RegisterURLArgsFunction( functionRef )
  163. {
  164.     URLArgsFunctionList[URLArgsFunctionList.length] = functionRef;
  165. }
  166.  
  167. // -----------------------------------------------------------------------------
  168. // Add query parameters to a href.    The first set comes from known parameters
  169. // that must be passed by application servers.    The second is specified by
  170. // the arguments to the function (which must be name/value pairs)
  171.  
  172. function URLArgs()
  173. {
  174.     var args = new Array;
  175.     
  176.     var oldArgs = queryStringToArray( Request.QueryString.Item );
  177.     for( var x in oldArgs ){
  178.         if( x == "mscssid" ){ // add other known parameters here using ||
  179.             args[x] = oldArgs[x];
  180.         }
  181.     }
  182.     for( var i = 0; i < arguments.length; i += 2 ){
  183.         args[Server.URLEncode( arguments[i] )] = Server.URLEncode( arguments[i + 1] );
  184.     }
  185.     if( typeof(URLArgsFunctionList ) != "undefined" ){
  186.         for( var f = 0; f < URLArgsFunctionList.length; f++ ){
  187.             var moreArgs = URLArgsFunctionList[f]();
  188.             for( var x in moreArgs ){
  189.                 args[x] = moreArgs[x];
  190.             }
  191.         }
  192.     }
  193.     
  194.     return queryArrayToString( args );
  195. }
  196.  
  197. // -----------------------------------------------------------------------------
  198. // Split up a string of the form "arg1=val1&...&argN=valN" into an array
  199. // of argument value pairs.
  200.  
  201. function queryStringToArray( queryString )
  202. {
  203.     if( queryString == "" ){
  204.         return new Array;
  205.     }
  206.     var queryArray = new Array;
  207.     var args = queryString.split( "&" );
  208.     for( var i = 0; i < args.length; i++ ){
  209.         var pair = args[i].split( "=" );
  210.         queryArray[pair[0]] = pair[1];
  211.     }
  212.     return queryArray;
  213. }
  214.  
  215. // -----------------------------------------------------------------------------
  216. // Construct a query string from an array of key value pairs.
  217.  
  218. function queryArrayToString( queryArray )
  219. {
  220.     var args = new Array;
  221.     var i = 0
  222.     for( var x in queryArray ){
  223.         if( queryArray[x] == null ){
  224.             args[i] = x;
  225.         }else{
  226.             args[i] = x + "=" + queryArray[x];
  227.         }
  228.         i++;
  229.     }
  230.     return args.join( "&" );
  231. }
  232.  
  233. // -----------------------------------------------------------------------------
  234. // Turn an application-relative path into a file-relative path
  235.  
  236. function mapPath( path )
  237. {
  238.     var ups = -2;    // lose one for relative path, one for current directory
  239.  
  240.     var curPath = "" + Request.ServerVariables( "PATH_INFO" );
  241.     for( var i = 0; i < curPath.length; i++ ){
  242.         if( curPath.charAt( i ) == '/' || curPath.charAt( i ) == '\\' ){
  243.             ups++;
  244.         }
  245.     }
  246.  
  247.     var upPath = "";
  248.     for( var i = 0; i < ups; i++ ){
  249.         upPath += "../"
  250.     }
  251.     return upPath + path;
  252. }
  253.  
  254. // -----------------------------------------------------------------------------
  255. // Turn an relative path into an absolute path
  256.  
  257. function absolutePath(basePath, relativePath)
  258. {
  259.     if (String(relativePath).charAt(0) == '/') {
  260.         return relativePath;
  261.     }
  262.  
  263.     var origurl = String( basePath ).split( "?" );
  264.     var newurl = String( relativePath ).split( "?" );
  265.  
  266.     var origparts = String( origurl[0] ).split( "/" );
  267.     var newparts = String( newurl[0] ).split( "/" );
  268.  
  269.     var origargs;
  270.     if( origurl.length > 1 ){
  271.         origargs = String( origurl[1] ).split( "&" );
  272.     }else{
  273.         origargs = new Array();
  274.     }
  275.     var newargs;
  276.     if( newurl.length > 1 ){
  277.         newargs = String( newurl[1] ).split( "&" );
  278.     }else{
  279.         newargs = new Array();
  280.     }
  281.     
  282.     var finalparts;
  283.     var part = new String( newparts[0] );
  284.     if( part.charAt( part.length - 1 ) == ":" ) { // absolute href
  285.         finalparts = newparts;
  286.     }else{
  287.         origparts = origparts.slice( 0, -1 ); // first we'll get rid of the last part
  288.         while( newparts[0] == ".." ){
  289.             origparts = origparts.slice( 0, -1 ); // get rid of the last part
  290.             newparts = newparts.slice( 1 ); // get rid of the ".."
  291.         }
  292.         finalparts = origparts.concat( newparts );
  293.     }
  294.  
  295.     var finalargs = origargs.concat( newargs );
  296.     return finalparts.join( "/" ).concat( "?" ).concat( finalargs.join( "&" ));
  297. }
  298.  
  299. // -----------------------------------------------------------------------------
  300. // Add arguments to a url.    New values replace old values.
  301.  
  302. function addArgs( url, newArgs )
  303. {
  304.     var urlString = String( url );
  305.  
  306.     if( newArgs == "" ){
  307.         return urlString;
  308.     }
  309.     var i = urlString.indexOf( "?" );
  310.     if( i == -1 ){
  311.         return urlString + "?" + newArgs;
  312.     }
  313.     var urlBase = urlString.substr( 0, i );
  314.     var oldArgs = urlString.substr( i + 1 );
  315.     var values = queryStringToArray( oldArgs );
  316.     var newValues = queryStringToArray( newArgs );
  317.     for( var x in newValues ){
  318.         values[x] = newValues[x];
  319.     }
  320.     return urlBase + "?" + queryArrayToString( values );
  321. }    
  322.  
  323. // *****************************************************************************
  324. // RECORD NAVIGATION
  325. //
  326. // These functions support "prev" and "next" links on record-oriented content
  327. // sources.
  328. //
  329.  
  330. // -----------------------------------------------------------------------------
  331. // Get current RECORD_INDEX. It returns URL parameter, AbsolutePosition or 1 as default.
  332. // If BlockSize is not set, it returns 1.
  333. function GetRecordIndex( contentSource )
  334. {
  335.     var recordIndex = 1;
  336.     if( Request.QueryString( "RECORD_INDEX" ).Count >= 1 ){
  337.         recordIndex = Number( Request.QueryString( "RECORD_INDEX" ));
  338.     }else{
  339.         if( typeof( contentSource ) != "undefined" ){
  340.             if( 0 == contentSource.BlockSize )    recordIndex = contentSource.AbsolutePosition;
  341.             else                                recordIndex = 1;
  342.         }
  343.     }
  344.     return    recordIndex;
  345. }
  346.  
  347. // -----------------------------------------------------------------------------
  348. // Remove URL parameter from URL string
  349.  
  350. function removeUrlParameter( urlString, removeParameter )
  351. {
  352.     var returnString = "";
  353.     var x = urlString.indexOf( removeParameter );
  354.     if( 0 <= x ){
  355.         if( 0 <= x ){
  356.             returnString = urlString.substr( 0, x-1 );        // stuff before parameter
  357.         }
  358.         var y = urlString.indexOf( "&", x );
  359.         if( 0 < y ){
  360.             returnString += "&" + urlString.substr( y+1 );    // stuff after parameter
  361.         }
  362.     }else{
  363.         returnString = urlString;
  364.     }
  365.     return    returnString;
  366. }
  367.  
  368.  
  369. // -----------------------------------------------------------------------------
  370. // Create a link to the current page with a specified record index, preserving
  371. // all other query parameters.
  372.  
  373. function linkToRecord( contentSource, index )
  374. {
  375.     var href = Request.ServerVariables( "URL" ) + "?RECORD_INDEX=" + index;
  376.     var queryString = String( Request.QueryString );
  377.     queryString = removeUrlParameter( queryString, "$key" );
  378.     queryString = removeUrlParameter( queryString, "RECORD_KEY" );
  379.     for( var i = 0; i < contentSource.PrimaryKey.length; i++ ) queryString = removeUrlParameter( queryString, contentSource.PrimaryKey[i] );
  380.  
  381.     var a = queryString.indexOf( "RECORD_INDEX" );
  382.     if( a == -1 ){
  383.         a = queryString.indexOf( "RECORD%5FINDEX" );      // URL-encoded '_'
  384.     }
  385.     if( a == -1 ){
  386.         href += "&" + queryString;
  387.     }else{
  388.         if( a > 0 ){
  389.             href += "&" + queryString.substr( 0, a-1 );      // stuff before RECORD_INDEX
  390.         }
  391.         var b = queryString.indexOf( "&", a );
  392.         if( b > 0 ){
  393.             href += "&" + queryString.substr( b+1 );      // stuff after RECORD_INDEX
  394.         }
  395.     }
  396.  
  397.     return " href=\"" + href + "\"";
  398. }
  399.  
  400.  
  401. // -----------------------------------------------------------------------------
  402. // Create a link to the prevoius record in a record-oriented content source.
  403.  
  404. function LinkToPreviousRecord( contentSource )
  405. {
  406.     var current = GetRecordIndex( contentSource );
  407.     var index = current - ( 0 == contentSource.BlockSize ? 1 : contentSource.BlockSize );
  408.     if( contentSource.RecordCount < index ) index = contentSource.RecordCount;
  409.     if( index < 1 ) return "";        // disable link...
  410.     else            return    linkToRecord( contentSource, index );
  411. }
  412.  
  413. // -----------------------------------------------------------------------------
  414. // Create a link to the next record in a record-oriented content source.
  415.  
  416. function LinkToNextRecord( contentSource )
  417. {
  418.     var current = GetRecordIndex( contentSource );
  419.     var index = current + ( 0 == contentSource.BlockSize ? 1 : contentSource.BlockSize );
  420.     if( index < 1 ) index = 1;
  421.     if( contentSource.RecordCount < index )        return    "";        // disable link...
  422.     else                                        return    linkToRecord( contentSource, index );
  423. }
  424.  
  425. // -----------------------------------------------------------------------------
  426. // Create a link to a new record in a record-oriented content source.
  427.  
  428. function LinkToNewRecord(contentSource)
  429. {
  430.     return linkToRecord( contentSource, contentSource.RecordCount + 1 );
  431. }
  432.  
  433. // -----------------------------------------------------------------------------
  434. // Create a link to the first record in a record-oriented content source.
  435.  
  436. function LinkToFirstRecord( contentSource )
  437. {
  438.     var current = GetRecordIndex( contentSource );
  439.     if( current <= 1 )    return    ""; // disable link...
  440.     else                return linkToRecord( contentSource, 1 );
  441. }
  442.  
  443. // -----------------------------------------------------------------------------
  444. // Create a link to the last record in a record-oriented content source.
  445.  
  446. function LinkToLastRecord( contentSource )
  447. {
  448.     var current = GetRecordIndex( contentSource );
  449.     var index = contentSource.RecordCount;
  450.     if( 0 < contentSource.BlockSize )    index = Math.floor( contentSource.RecordCount /     contentSource.BlockSize ) * contentSource.BlockSize + 1;
  451.     if(( index <= current )||( contentSource.RecordCount < index ))    return    ""; // disable link...
  452.     else    return    linkToRecord( contentSource, index );
  453. }
  454.  
  455. // *****************************************************************************
  456. // MISCELLANEOUS
  457.  
  458. function Selected( condition )
  459. {
  460.     if( condition ){
  461.         return "selected";
  462.     }else{
  463.         return "";
  464.     }
  465. }
  466.  
  467. function Checked( condition )
  468. {
  469.     if( condition ){
  470.         return "checked";
  471.     }else{
  472.         return "";
  473.     }
  474. }
  475.  
  476. // *****************************************************************************
  477. // HTML ENCODING/DECODING FOR VBSCRIPT
  478.  
  479. function encodeHTML( text )
  480. {
  481.     return urlencode( text );
  482. }
  483.  
  484. function decodeHTML( text )
  485. {
  486.     return unescape( text );
  487. }
  488.  
  489.  
  490. // -----------------------------------------------------------------------------
  491. // Encode a string in application/x-www-form-urlencoded form.  TODO: this
  492. // function leaves somes non-alphanumeric characters unencoded, but it
  493. // correctly encodes '+', '&', and '=', and so works correctly with the
  494. // ASP query string decoding.
  495.  
  496. function urlencode( text )
  497. {
  498.     var encodedText = escape( text );
  499.     encodedText = encodedText.replace( /\+/g, "%2B" );
  500.     return encodedText;
  501. }
  502.  
  503. // *****************************************************************************
  504. // SECURITY
  505.  
  506. function RejectUnauthorizedCallers()
  507. {
  508.     var caller = String( Request.ServerVariables( "REMOTE_ADDR" )).split( "." );
  509.     var fso = Server.CreateObject( "Scripting.FileSystemObject" );
  510.     var currentFile = fso.OpenTextFile( Request.ServerVariables( "PATH_TRANSLATED" ));
  511.     var matchExpression = /\s*[<]!--\s*#INCLUDE\s+(FILE|VIRTUAL)="(.*)[\/]include[\/]utils.runtime5.asp"\s*--[>]/;
  512.     while(!currentFile.AtEndOfStream) {
  513.         if(matchExpression.exec( currentFile.ReadLine() )) {
  514.             var friendsPath;
  515.             if( RegExp.$1 == "FILE" ){
  516.                 friendsPath = Server.MapPath( "." ) + "/" + RegExp.$2 
  517.                     +( RegExp.$2 != "" ? "/":"" )
  518.                     + "include/friends.asp";
  519.             }else{
  520.                 friendsPath = Server.MapPath( RegExp.$2 )
  521.                     +( RegExp.$2 != "" ? "/":"" )
  522.                     + "include/friends.asp";
  523.             }
  524.  
  525.             if( fso.FileExists( friendsPath )){
  526.                 var ts = fso.OpenTextFile( friendsPath, 1 ); // forReading
  527.                 while( !ts.AtEndOfStream ){
  528.                     var line = ts.ReadLine();
  529.                     if(( line.length == 0 )||( line.charAt( 0 ) == ";" )||( line.charAt( 0 ) == "<" )){
  530.                         continue;
  531.                     }
  532.                     var splitline = line.split(/\s/);
  533.                     var kosherAddr = splitline[0].split( "." );
  534.                     var kosherMask = ( splitline.length > 1 ) ? splitline[1].split( "." ) : new Array( 255, 255, 255, 255 );
  535.                     var kosher = true;
  536.                     var i;
  537.                     for( i in caller ) {
  538.                         if(kosherAddr[i] != ( caller[i] & kosherMask[i] )) {
  539.                             kosher = false;
  540.                             break;
  541.                         }
  542.                     }
  543.                     if( kosher ){
  544.                         return;
  545.                     }
  546.                 }
  547.             }else if(( caller[0] == 127 )&&( caller[1] == 0 )&&( caller[2] == 0 )&&( caller[3] == 1 )){
  548.                 return;
  549.             }
  550.             break;
  551.         }
  552.     }
  553.  
  554.     Response.Status = "403 Forbidden";
  555.     Response.Write( "You are not authorized to access this page. Check with your Web administrator to make sure your machine is listed in the include/friends.asp file." );
  556.     Response.End();
  557. }
  558.  
  559.  
  560. // *****************************************************************************
  561. // For version 1.0 compatibility
  562.  
  563. function linkToPreviousRecord( contentSource ){    return LinkToPreviousRecord( contentSource );    }
  564. function linkToNextRecord(       contentSource ){    return LinkToNextRecord(     contentSource );    }
  565. function linkToNewRecord(       contentSource ){    return LinkToNewRecord(         contentSource );    }
  566. function linkToFirstRecord(       contentSource ){    return LinkToFirstRecord(     contentSource );    }
  567. function linkToLastRecord(       contentSource ){    return LinkToLastRecord(     contentSource );    }
  568. function selected( condition ){    return    Selected( condition );    }
  569. function checked(  condition ){    return    Checked(  condition );    }
  570.  
  571. // *****************************************************************************
  572. // Runtime Debug Message
  573.  
  574. function RuntimeDebugMessage( msg )
  575. {
  576.     Response.Write( "<b><font color=\"red\">Runtime Error: " + msg + "</font></b><br>\n" );
  577.     return;
  578. }
  579.  
  580. </SCRIPT>
  581.  
  582. <SCRIPT runat="server" language="VBScript">
  583.  
  584. ' -----------------------------------------------------------------------------
  585. ' Pick a random element from a comma separated list of choices.
  586.  
  587. function random(choices)
  588.     Randomize
  589.     choices = choices & ","
  590.     choice = ""
  591.     n = 0
  592.     p = 1
  593.     while p <> -1
  594.         q = InStr(p, choices, ",")
  595.         if q <> 0 then
  596.             n = n + 1
  597.             if Rnd() < 1/n then
  598.                 choice = Mid(choices, p, q - p)
  599.             end if
  600.             p = q + 1
  601.         else
  602.             p = -1
  603.         end if
  604.     wend
  605.     random = choice
  606. end function
  607.  
  608.  
  609. ' -----------------------------------------------------------------------------
  610. ' VBScript style DateTime format conversion for calling from JScript
  611. ' return string type
  612.  
  613. function callVBFormatDateTime( dateField )
  614.  
  615.     callVBFormatDateTime = FormatDateTime( dateField.Value, 0 )
  616.  
  617. end function
  618.  
  619.  
  620. ' -----------------------------------------------------------------------------
  621. ' Encode single/double quotation mark as HTML encode
  622. function fixHTMLquotes( text )
  623.     if IsNull( text ) then
  624.         fixHTMLquotes = ""
  625.     else
  626.         temp = Replace( text, """", """ )
  627.         fixHTMLquotes = Replace( temp, "'", "'" )
  628.     end if
  629. end function
  630.  
  631.  
  632. ' -----------------------------------------------------------------------------
  633.  
  634. </SCRIPT>
  635.  
  636.